const xlsx = require('xlsx');
const node_xlsx = require('node-xlsx');
const fs = require('fs');
const _ = require('lodash');
var iconv = require('iconv-lite');

let filePath = './rs.xlsx';

// let result = xlsx.parse(filePath);

let rs = fs.createReadStream(filePath);

process_RS(rs, wb => {
    let first_sheet_name = wb.SheetNames[0];
    let ws = wb.Sheets[first_sheet_name];

    let result = {};
    let typeMap = {};

    for (let m in ws) {
        /* all keys that do not begin with "!" correspond to cell addresses */
        if (m[0] === '!') continue;
        let value = ws[m].w;
        let values = value.split(/;+/);

        let type = m.match(/([A-Z])(.*)/);
        if(!type) continue;
        if(type[2] == 1){
            typeMap[type[1]] = value;
        }else{
            type = type[1];
            for (let v of values) {
                if (v) {
                    if(!result[type]){
                        result[type] = {};
                    }
                    if (!result[type][v]) {
                        result[type][v] = {
                            keyword: v,
                            count: 0
                        }
                    }
                    result[type][v].count++;
                }
            }
        }
        // checkIds.push(ws[m].w);
    }

    let xlsData = [];
    for(let type in typeMap){
        let tmp = result[type];
        let re = _.map(tmp, (v, k) => v);
        let orderedR = _.orderBy(re, ['count'],['desc']);

        xlsData.push({
            sheet : typeMap[type],
            title : [{
                key : 'keyword',
                name : typeMap[type]
            },{
                key : 'count',
                name : '频次'
            }],
            data : orderedR
        });
    }

    let buffer = require('./xlsUtils').buildExcelResultBuf(xlsData);
    fs.writeFileSync('output.xlsx', buffer);
    console.log('文档以输出，请查阅。');
});

function process_RS(stream/*:ReadStream*/, cb/*:(wb:Workbook)=>void*/)/*:void*/{
    var buffers = [];
    stream.on('data', function(data) { buffers.push(data); });
    stream.on('end', function() {
        var buffer = Buffer.concat(buffers);
        // buffer = iconv.decode(buffer,'gbk');
        var workbook = xlsx.read(buffer, {type:"buffer"});

        /* DO SOMETHING WITH workbook IN THE CALLBACK */
        cb(workbook);
    });
}

function xlsxParser(file, handler) {
    var result = {code: 1, msg: ''};

    if (!file.originalname.endsWith('xlsx') && !file.originalname.endsWith('xls')) {
        result.code = 0;
        result.msg = '上传文件格式不正确，请上传excel文件';
        return result;
    }
    var arr = [];
    for (var i = 0; i != file.buffer.length; ++i) {
        arr[i] = String.fromCharCode(file.buffer[i]);
    }
    var bstr = arr.join('');

    var wb = XLSX.read(bstr, {type: 'binary'});

    var first_sheet_name = wb.SheetNames[0];

    /* Get worksheet */
    var ws = wb.Sheets[first_sheet_name];
    return handler(ws);
}

